import Eureka
//添加教育表单

class addEducationController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的resumeId
    var resumeId:String = String()

	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        
        
        
        form +++ Section("")
            <<< TextRow("schoolName"){ row in
                row.title = "学校名称"
                row.placeholder = "输入名称"
            }
            <<< TextRow("major"){ row in
                row.title = "所学专业"
                row.placeholder = "输入名称"
            }
            <<< PushRow<String>("degree"){
                $0.title = "学历"
                $0.selectorTitle = "学历"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_DEGREE)
                $0.options = array
                $0.value = array[0]
            }
        +++ Section()
            <<< DateRow("startdate"){
                    $0.title = "开始时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }
            <<< DateRow("enddate"){
                    $0.title = "结束时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }
        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = Optional("添加教育经历")
                    row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                    row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })


    }


    private func submitForm(){
        // 等待提示
//        waiting.show(self)
        //获取表单结果
        let dict = form.values()

        //由segue传过来的resumeId
        let resumeId = self.resumeId

        let schoolName = dict["schoolName"] as! String
        let major = dict["major"] as! String
        let degree = enumUtil.getKeyByString(dict["degree"] as! String,dict: enumUtil.RESUME_DEGREE)

        let startdate = dict["startdate"] as! NSDate
        let enddate = dict["enddate"] as! NSDate

        resumeManager.AddEducation(resumeId,
                             startDate:startdate,
                             endDate:enddate,
                             schoolName:schoolName,
                             major:major,
                             degree:degree,
                             successCallback: { (msg) in
                    //            添加信息ok
//                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


